home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Fatted Calf
/
The Fatted Calf.iso
/
Applications
/
Games
/
NeXTGo
/
Source
/
smartgotree.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-02-08
|
6KB
|
401 lines
#include "comment.header"
#include "smartgo.h"
/* Define the following variable for debugging information. */
/* #define _DEBUG_ON_ */
#ifdef _DEBUG_ON_
#include <stdio.h>
void display_tree(node* root_node)
{
node *tnode;
tnode = root_node;
while (tnode != NULL)
{
if (tnode->properties != NULL)
printf(";");
if (tnode->variants != NULL)
{
printf("(");
display_tree(tnode->variants);
printf(")");
}
if (tnode->next == NULL)
{
while (tnode->prev != NULL)
tnode = tnode->prev;
tnode = tnode->next_var;
if (tnode != NULL)
printf(")(");
}
else
{
tnode = tnode->next;
}
}
}
#endif
node* forwardOneNode(node* currentNode)
{
node *tnode;
if (currentNode->variants != NULL)
{
return currentNode->variants;
}
else if (currentNode->next != NULL)
{
return currentNode->next;
}
else
{
tnode = currentNode;
while (tnode->prev != NULL)
tnode = tnode->prev;
if (tnode->next_var != NULL)
{
return tnode->next_var;
}
else
{
return forwardOneNode0(currentNode->parent);
}
}
}
node* forwardOneNode0(node* currentNode)
{
node *tnode;
if (currentNode->next != NULL)
{
return currentNode->next;
}
else
{
tnode = currentNode;
while (tnode->prev != NULL)
tnode = tnode->prev;
if (tnode->next_var != NULL)
{
return tnode->next_var;
}
else
{
if (currentNode->parent != NULL)
{
return forwardOneNode0(currentNode->parent);
}
else
{
return tnode;
}
}
}
}
node* backOneNode(node* currentNode)
{
if (currentNode->prev != NULL)
{
if (currentNode->prev->variants != NULL)
{
return findLast(currentNode->prev->variants);
}
else
{
return currentNode->prev;
}
}
else
{
if (currentNode->prev_var != NULL)
{
return findLast0(currentNode->prev_var);
}
else
{
if (currentNode->parent != NULL)
{
return currentNode->parent;
}
else
{
return findLast(currentNode);
}
}
}
}
node* findLast(node* currentNode)
{
node *tnode;
tnode = currentNode;
while (tnode->next_var != NULL)
tnode = tnode->next_var;
while (tnode->next != NULL)
tnode = tnode->next;
if (tnode->variants != NULL)
{
return findLast(tnode->variants);
}
else
{
return tnode;
}
}
node* findLast0(node* currentNode)
{
node *tnode;
tnode = currentNode;
while (tnode->next != NULL)
tnode = tnode->next;
if (tnode->variants != NULL)
{
return findLast(tnode->variants);
}
else
{
return tnode;
}
}
node* forwardOneVariant(node* currentNode)
{
node *tnode;
tnode = currentNode;
while (tnode->prev != NULL)
tnode = tnode->prev;
if (tnode->next_var != NULL)
{
return tnode->next_var;
}
else
{
return tnode->parent->variants;
}
}
node* backOneVariant(node* currentNode)
{
node *tnode;
tnode = currentNode;
while (tnode->prev != NULL)
tnode = tnode->prev;
if (tnode->prev_var != NULL)
{
return tnode->prev_var;
}
else
{
while (tnode->next_var != NULL)
tnode = tnode->next_var;
return tnode;
}
}
void clearNodeFlags(node* currentNode)
{
node *r, *v;
r = currentNode;
while (r != NULL)
{
r->flag = 0;
v = r->variants;
while (v != NULL)
{
clearNodeFlags(v);
v = v->next_var;
}
r = r->next;
}
}
int foundNode;
int evaluateSteps(node* currentNode, node* targetNode, unsigned char b[19][19])
{
node *tnode, *vnodes;
int i, j;
extern int MAXX, MAXY;
unsigned char b0[19][19];
for (i = 0; i < 19; i++)
for (j = 0; j < 19; j++)
b0[i][j] = b[i][j];
tnode = currentNode;
while (tnode != targetNode)
{
if (tnode->properties != NULL)
{
evaluateNode(tnode->properties, b0);
tnode->flag = 1;
}
vnodes = tnode->variants;
while (vnodes != NULL)
{
evaluateSteps(vnodes, targetNode, b0);
if (foundNode)
{
for (i = 0; i < MAXX; i++)
for (j = 0; j < MAXY; j++)
b[i][j] = b0[i][j];
return 0;
}
vnodes = vnodes->next_var;
}
if (tnode->next == NULL)
return 0;
tnode = tnode->next;
}
if (tnode == targetNode)
{
foundNode = 1;
}
if ((tnode == targetNode) && (!tnode->flag) && (tnode->properties != NULL))
{
evaluateNode(tnode->properties, b0);
tnode->flag = 1;
for (i = 0; i < MAXX; i++)
for (j = 0; j < MAXY; j++)
b[i][j] = b0[i][j];
}
return 0;
}
void buildToNode(node* targetNode)
{
int i, j;
extern int blackCaptured, whiteCaptured;
extern unsigned char p[19][19];
extern node* rootNode;
for (i = 0; i < 19; i++)
for (j = 0; j < 19; j++)
p[i][j] = 0;
blackCaptured = whiteCaptured = 0;
foundNode = 0;
clearNodeFlags(rootNode);
evaluateSteps(rootNode, targetNode, p);
}
node* stepForward(node* currentNode)
{
node *tnode;
extern node *rootNode;
tnode = currentNode;
do
{
tnode = forwardOneNode(tnode);
}
while ((tnode->properties == NULL) && (tnode != currentNode));
if (tnode == currentNode)
{
tnode = rootNode;
do
{
tnode = forwardOneNode(tnode);
}
while (tnode->properties == NULL);
}
buildToNode(tnode);
return tnode;
}
node* stepBackward(node* currentNode)
{
node *tnode;
extern node *rootNode;
tnode = currentNode;
do
{
tnode = backOneNode(tnode);
}
while ((tnode->properties == NULL) && (tnode != currentNode));
if (tnode == currentNode)
{
tnode = rootNode;
tnode = findLast(tnode);
}
buildToNode(tnode);
return tnode;
}
node* jumpForward(node* currentNode)
{
node *tnode;
tnode = currentNode;
tnode = forwardOneVariant(tnode);
while (tnode->properties == NULL)
{
tnode = forwardOneNode(tnode);
}
buildToNode(tnode);
return tnode;
}
node* jumpBackward(node* currentNode)
{
node *tnode;
tnode = currentNode;
tnode = backOneVariant(tnode);
while (tnode->properties == NULL)
{
tnode = forwardOneNode(tnode);
}
buildToNode(tnode);
return tnode;
}